package sophie.map;

import java.io.Serializable;
import java.util.AbstractList;
import java.util.List;
import java.util.RandomAccess;

public class MyArrayList<E> extends AbstractList<E> 
	implements List<E>, RandomAccess, Serializable, Cloneable {

	private static final long serialVersionUID = 5997760245000209215L;

	private Object[] table;
	
	private int size;
	
	public MyArrayList() {
		this.table = new Object[16];
	}
	
	@Override
	public E get(int index) {
		checkIndex(index);
		return getElement(index);
	}
	
	private void checkIndex(int index) {
		if(index < 0 || index >= size) {
			throw new IndexOutOfBoundsException();
		}
	}
	
	@SuppressWarnings("unchecked")
	private E getElement(int index) {
		return (E) table[index];
	}

	@Override
	public void add(int index, E element) {
		table[size++] = element;
		modCount++;
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}

	@Override
	public int size() {
		return size;
	}

}
